'use strict';

var Pili = require('./lib/index.js');
var apis = require('../lib/apis.js');
var util = require('./util')

// ======================== Configurations =========================
// Replace with your keys here
var ACCESS_KEY  = apis['qiniu']['AK'];
var SECRET_KEY  = apis['qiniu']['SK'];
var credentials = new Pili.Credentials(ACCESS_KEY, SECRET_KEY);


// Replace with your hub name
var HUB = apis['qiniu']['domain']; // The Hub must be exists before use

// Change API host as necessary
//
// pili.qiniuapi.com as default
// pili-lte.qiniuapi.com is the latest RC version
//
Pili.config.API_HOST = 'pili.qiniuapi.com'; // default

// ========================== Hub ============================



/**
 * URLs
 */


exports.getPushIO = function(key){
	//return Pili.publishURL(credentials,apis['qiniu']['publish_url'],HUB, key, 60);
}
exports.getpullIO = function(key){
	//return Pili.rtmpPlayURL(apis['qiniu']['rtmp_url'],HUB, key);
}

//var pu = Pili.publishURL(credentials,'publish-rtmp.test.com', 'PiliSDKTest', 'streamkey', 60);
//var rtmpURL = Pili.rtmpPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
//var hdlURL = Pili.hdlPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
//var hlsURL = Pili.hlsPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
//var snapURL = Pili.snapshotPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
//console.log(pu, rtmpURL, hdlURL, hlsURL, snapURL);
//
///**
// * Instantiate a Pili hub object
// */
//
var hub = new Pili.Hub(credentials, HUB);

/**
 * Create a new Stream
 */


exports.createStream = function(id,callback) {
	var options = {
		title          : id,    // optional, auto-generated as default
		publishKey     : util.urlsafeBase64Encode(id),    // optional, auto-generated as default
		publishSecrity : "static" // optional, can be "dynamic" or "static", "dynamic" as default
	};

	hub.createStream(options, function(err, stream) {
		if (!err) {
			callback(err,stream.id,stream.createdAt,stream.rtmpPublishUrl());
		} else {
			callback(err,null,null);
		}
	});
}


exports.getStream = function(streamKey,callback) {
	hub.getStream(streamKey, function(err, stream) {
		if (!err) {
			callback(err,stream);
		} else {
			callback(err,stream);
		}
	});
}


exports.saveas = function(streamKey,callback) {
	hub.saveas(util.urlsafeBase64Encode(streamKey), function(err, stream) {
		if (!err) {
			callback(err,stream);
		} else {
			callback(err,stream);
		}
	});
}


exports.saveasMp4 = function(streamKey,callback) {
	hub.saveasMp4(util.urlsafeBase64Encode(streamKey), function(err, stream) {
		if (!err) {
			callback(err,stream);
		} else {
			callback(err,stream);
		}
	});
}



//
///**
// * List Streams
// */
//
//var listOptions = {
//	'liveonly': false,
//	'prefix': 'lin',
//	'limit': 2,
//};
//
//var listCallBack = function(err, marker, streams) {
//	if (!err) {
//		streams.forEach(function(stream) {
//			console.log(stream);
//		});
//		if (marker) {
//			listOptions.marker = marker;
//			hub.listStreams(listOptions, listCallBack);
//		}
//	} else {
//    console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
//	}
//}
//
//hub.listStreams(listOptions, listCallBack);
//
///**
// * Get Stream Info
// */
//var stream = hub.newStream(streamKey);
//
//stream.loadInfo(function(err) {
//	if (!err) {
//		console.log(stream);
//	} else {
//    console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
//	}
//})
//
//// * Disable a Stream
//// */
//stream.disable(-1, function(err) {
//    console.log(stream.disabledTill);
//});
//
//
///**
// * Enable a Stream
// */
//stream.enable(function(err) {
//    console.log(stream.disabledTill);
//});
//
//
///**
// * Get Stream Live Info
// */
//stream.liveInfo(function(err, status) {
//    if (!err) {
//			console.log(status);
//		} else {
//			console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
//		}
//});
//
///**
// * Get Stream segments
// */
//var savePlaybackOptions = {
//   start : null,    // optional, in second, unix timestamp
//   end   : null,    // optional, in second, unix timestamp
//};
//
//stream.savePlayback(savePlaybackOptions, function(err, m3u8Name) {
//
//    if (!err) {
//			console.log(m3u8Name);
//		} else {
//			console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
//		}
//});
//
///**
// *  Get Stream history activity
// */
//
//var publishHistoryOptions = {
//   start : null,    // optional, in second, unix timestamp
//   end   : null,    // optional, in second, unix timestamp
//};
//stream.publishHistory(publishHistoryOptions, function(err, history) {
//	if (!err) {
//		console.log(history);
//	} else {
//			console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
//	}
//})
